﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.ObjectModel;
using G.Data;

namespace G.Controls
{
    public enum GridListBox_RowItemWidthType
    {
        AutoSize,//自适应
        Custom//自定义
    }
    public class GridListBox : ListBox
    {

        public static readonly DependencyProperty CellContentTemplateProperty = DependencyProperty.Register("CellContentTemplate", typeof(DataTemplate), typeof(GridListBox), null);
        /// <summary>
        /// 内容元素模版
        /// </summary>
        public DataTemplate CellContentTemplate
        {
            get
            {
                return this.GetValue(CellContentTemplateProperty) as DataTemplate;
            }
            set
            {
                this.SetValue(CellContentTemplateProperty, value);
            }
        }


        public static readonly DependencyProperty CellContentWidthProperty = DependencyProperty.Register("CellContentWidth", typeof(double), typeof(GridListBox), null);
        /// <summary>
        /// 内容元素的宽度
        /// </summary>
        public double CellContentWidth
        {
            get
            {
                return (double)this.GetValue(CellContentWidthProperty);
            }
            set
            {
                this.SetValue(CellContentWidthProperty, value);
            }
        }

        public static readonly DependencyProperty CellItemStyleProperty = DependencyProperty.Register("CellItemStyle", typeof(Style), typeof(GridListBox), null);
        /// <summary>
        /// 内容元素的样式
        /// </summary>
        public Style CellItemStyle
        {
            get
            {
                return (Style)this.GetValue(CellItemStyleProperty);
            }
            set
            {
                this.SetValue(CellItemStyleProperty, value);
            }
        }


        public static readonly DependencyProperty RowItemWidthTypeProperty = DependencyProperty.Register("RowItemWidthType", typeof(GridListBox_RowItemWidthType), typeof(GridListBox), null);
        /// <summary>
        /// 行内元素的宽度类型
        /// </summary>
        public GridListBox_RowItemWidthType RowItemWidthType
        {
            get
            {
                return (GridListBox_RowItemWidthType)this.GetValue(RowItemWidthTypeProperty);
            }
            set
            {
                this.SetValue(RowItemWidthTypeProperty, value);
            }
        }

        public static readonly DependencyProperty CellItemSelectedCommandProperty = DependencyProperty.Register("CellItemSelectedCommand", typeof(ICommand), typeof(GridListBox), null);

        public ICommand CellItemSelectedCommand
        {
            get
            {
                return this.GetValue(CellItemSelectedCommandProperty) as ICommand;
            }
            set
            {
                this.SetValue(CellItemSelectedCommandProperty, value);
            }
        }

        public static readonly DependencyProperty GridItemsSourceProperty = DependencyProperty.Register("GridItemsSource", typeof(GridListBoxDataSource), typeof(GridListBox), new PropertyMetadata(null, GridItemsSourcePropertyChanged));

        private static void GridItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            GridListBox data = d as GridListBox;
            if (data.GridItemsSource != null)
                data.GridItemsSource.GridListBox = data;
        }

        public GridListBoxDataSource GridItemsSource
        {
            get
            {
                return this.GetValue(GridItemsSourceProperty) as GridListBoxDataSource;
            }
            set
            {
                this.SetValue(GridItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty RowItemSizeProperty = DependencyProperty.Register("RowItemSize", typeof(int), typeof(GridListBox), new PropertyMetadata(2));
        /// <summary>
        /// 每行宫格数量，默认为2个
        /// </summary>
        public int RowItemSize
        {
            get
            {
                return (int)this.GetValue(RowItemSizeProperty);
            }
            set
            {
                this.SetValue(RowItemSizeProperty, value);
            }
        }

        public GridListBox()
        {
            this.DefaultStyleKey = typeof(GridListBox);
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new CellRowItem();
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            CellRowItem list = element as CellRowItem;
            list.HostListBox = this;
            list.ArrayItems = item as Array;
        }


        public event CellItemSelected CellItemSelected;

        internal void ItemSelected(object cellItem, object item)
        {
            if (this.CellItemSelectedCommand != null)
            {
                this.CellItemSelectedCommand.Execute(item);
            }
            if (CellItemSelected != null)
                CellItemSelected(cellItem, new CellItemSelectedArgs() { SelectedItem = item });
        }
    }

    public delegate void CellItemSelected(object sender, CellItemSelectedArgs e);

    public class CellItemSelectedArgs : EventArgs
    {
        public object SelectedItem { get; internal set; }
    }
}
